package cn.itrip.controller;

import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.pojo.ItripAreaDic;
import cn.itrip.beans.pojo.ItripImage;
import cn.itrip.beans.pojo.ItripLabelDic;
import cn.itrip.beans.vo.ItripAreaDicVO;
import cn.itrip.beans.vo.ItripImageVO;
import cn.itrip.beans.vo.ItripLabelDicVO;
import cn.itrip.beans.vo.hotel.*;
import cn.itrip.common.DtoUtil;
import cn.itrip.common.EmptyUtils;
import cn.itrip.common.ErrorCode;
import cn.itrip.service.itripAreaDic.ItripAreaDicService;
import cn.itrip.service.itripHotel.ItripHotelService;
import cn.itrip.service.itripImage.ItripImageService;
import cn.itrip.service.itripLabelDic.ItripLabelDicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName HotelController
 * @Description TODO
 * @Author yaozhaobao
 * @Date 2020/12/10 15:50
 *
 *  * 包括API接口：
 *  * 1、根据酒店id查询酒店拓展属性
 *  * 2、根据酒店id查询酒店介绍，酒店政策，酒店设施
 *  * 3、根据酒店id查询酒店特色属性列表
 *  * 4、根据type 和target id 查询酒店图片
 *  * 5、查询热门城市
 *  * 6、查询热门商圈列表（搜索-酒店列表）
 *  * 7、查询数据字典特色父级节点列表（搜索-酒店列表）
 *  * 8、根据酒店id查询酒店特色、商圈、酒店名称（视频文字描述）
 *
 * @Version 1.0
 **/
@RestController
@Api(value = "/hotel",basePath = "http://127.0.0.1:8080/biz/api/hotel")
@RequestMapping("/api/hotel")
public class HotelController {

    Logger logger = Logger.getLogger("HotelController.class");
    @Autowired
    ItripAreaDicService itripAreaDicService ;

    @Autowired
    ItripImageService itripImageService;

    @Autowired
    ItripLabelDicService itripLabelDicService;
    @Autowired
    ItripHotelService itripHotelService;

    @ApiOperation(value = "根据id查询酒店信息",httpMethod ="Get",
            protocols = "http",
            produces = "application/json",
            response = Dto.class,
            notes = "sucess=true 获取数据成功  sucess=false 失败，错误码：120001:没有输入酒店id")
    @RequestMapping(value = "/info",method = RequestMethod.GET,produces = {"application/json"})
    public Dto getHotelInfoById(@ApiParam(required = true,value = "酒店id",name = "id") @RequestParam("id")Integer id){
        HotelVideoDescVO hotelVideoDescVO =new HotelVideoDescVO();
        hotelVideoDescVO.setHotelName("亚朵酒店");
       return DtoUtil.returnDataSuccess(hotelVideoDescVO);
    }


    @ApiOperation(value = "添加一个酒店信息",httpMethod ="POST",
            protocols = "http",
            produces = "application/json",
            response = Dto.class,
            notes = "sucess=true 获取数据成功  sucess=false 失败，错误码：120001:没有输入酒店信息")
    @RequestMapping(value = "/addHotel",method = RequestMethod.POST,produces = {"application/json"})
    public Dto addHotel(@ApiParam(required = true,value = "酒店对象",name = "hotel") @RequestBody HotelVideoDescVO hotelVideoDescVO){
        return DtoUtil.returnDataSuccess(hotelVideoDescVO);
    }


    @RequestMapping(value = "/querytradearea/{cityId}",method = RequestMethod.GET,produces = {"application/json"})
    public Dto<ItripAreaDicVO> queryTradeArea(@PathVariable  Integer cityId){
        //校验入参
        if(EmptyUtils.isEmpty(cityId)){
            return DtoUtil.returnFail("入参错误", ErrorCode.HOTEL_ILLEGAL_ARGS);
        }
        //f封装查询条件
        Map<String,Object> param=new HashMap<>();
        param.put("isTradingArea","1");
        param.put("parent",cityId);
        List<ItripAreaDicVO> result=new ArrayList<>();
        try {
            //调用接口ItripAreaDicService
            List<ItripAreaDic> itripAreaDic= itripAreaDicService.getItripAreaDicListByMap(param);
            //pojo------:>vo ----->前端
            //把商圈信息封装成ItripAreaDicVO,返回数据
            if(EmptyUtils.isNotEmpty(itripAreaDic)){
                for(ItripAreaDic itripAreaDic1:itripAreaDic){
                    ItripAreaDicVO itripAreaDicVO = new ItripAreaDicVO();
                    BeanUtils.copyProperties(itripAreaDic1,itripAreaDicVO);
                    result.add(itripAreaDicVO);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("数据库异常",ErrorCode.HOTEL_SQLEXCEPTION);

        }
        return DtoUtil.returnDataSuccess(result);
    }



    @RequestMapping(value = "/queryhotcity/{type}",method = RequestMethod.GET,produces = {"application/json"})
    public Dto<ItripAreaDicVO> queryhotelCity(@PathVariable Integer type){

        //校验入参
        if(EmptyUtils.isEmpty(type)){
            return DtoUtil.returnFail("入参错误", ErrorCode.HOTEL_ILLEGAL_ARGS);
        }
        //f封装查询条件
        Map<String,Object> param=new HashMap<>();
        param.put("isHot","1");
        param.put("isChina",type);
        List<ItripAreaDicVO> result=new ArrayList<>();
        try {
            //调用接口ItripAreaDicService
            List<ItripAreaDic> itripAreaDic= itripAreaDicService.getItripAreaDicListByMap(param);
            //pojo------:>vo ----->前端
            //把商圈信息封装成ItripAreaDicVO,返回数据
            if(EmptyUtils.isNotEmpty(itripAreaDic)){
                for(ItripAreaDic itripAreaDic1:itripAreaDic){
                    ItripAreaDicVO itripAreaDicVO = new ItripAreaDicVO();
                    BeanUtils.copyProperties(itripAreaDic1,itripAreaDicVO);
                    result.add(itripAreaDicVO);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("数据库异常",ErrorCode.HOTEL_SQLEXCEPTION);
        }
        return DtoUtil.returnDataSuccess(result);
    }


    @RequestMapping(value = "/getimg/{targetId}",method = RequestMethod.GET,produces = {"application/json"})
    public Dto<ItripImageVO> getImgForHotel(@PathVariable  String targetId){
        //校验入参
        if(EmptyUtils.isEmpty(targetId)){
            return DtoUtil.returnFail("入参错误", ErrorCode.HOTEL_ILLEGAL_ARGS);
        }
        //f封装查询条件
        Map<String,Object> param=new HashMap<>();
        param.put("type","0");
        param.put("targetId",targetId);
        List<ItripImageVO> result=new ArrayList<>();
        try {
            //调用接口ItripAreaDicService
            List<ItripImage> itripImages= itripImageService.getItripImageListByMap(param);
            //pojo------:>vo ----->前端
            //把商圈信息封装成ItripAreaDicVO,返回数据
            if(EmptyUtils.isNotEmpty(itripImages)){
                for(ItripImage itripImage:itripImages){
                    ItripImageVO itripImageVO = new ItripImageVO();
                    BeanUtils.copyProperties(itripImage,itripImageVO);
                    result.add(itripImageVO);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("数据库异常",ErrorCode.HOTEL_SQLEXCEPTION);
        }
        return DtoUtil.returnDataSuccess(result);
    }


    /***
     * 查询酒店特色列表
     *
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "查询酒店特色列表", httpMethod = "GET",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class, notes = "获取酒店特色(用于查询页列表)" +
            "<p>成功：success = ‘true’ | 失败：success = ‘false’ 并返回错误码，如下：</p>" +
            "<p>错误码: </p>" +
            "<p>10205: 系统异常,获取失败</p>")
    @RequestMapping(value = "/queryhotelfeature", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public Dto<ItripLabelDicVO> queryHotelFeature() {
        List<ItripLabelDic> itripLabelDics = null;
        List<ItripLabelDicVO> itripAreaDicVOs = null;
        try {
            Map param = new HashMap();
            param.put("parentId", 16);
            itripLabelDics = itripLabelDicService.getItripLabelDicListByMap(param);
            if (EmptyUtils.isNotEmpty(itripLabelDics)) {
                itripAreaDicVOs = new ArrayList();
                for (ItripLabelDic dic : itripLabelDics) {
                    ItripLabelDicVO vo = new ItripLabelDicVO();
                    BeanUtils.copyProperties(dic, vo);
                    itripAreaDicVOs.add(vo);
                }
            }

        } catch (Exception e) {
            DtoUtil.returnFail("系统异常", "10205");
            e.printStackTrace();
        }
        return DtoUtil.returnDataSuccess(itripAreaDicVOs);
    }


    @ApiOperation(value = "根据酒店id查询酒店特色、商圈、酒店名称", httpMethod = "GET",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class, notes = "根据酒店id查询酒店特色、商圈、酒店名称（视频文字描述）" +
            "<p>成功：success = ‘true’ | 失败：success = ‘false’ 并返回错误码，如下：</p>" +
            "<p>错误码：</p>" +
            "<p>100214 : 获取酒店视频文字描述失败 </p>" +
            "<p>100215 : 酒店id不能为空</p>")
    @RequestMapping(value = "/getvideodesc/{hotelId}", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Dto<Object> getVideoDescByHotelId(@ApiParam(required = true, name = "hotelId", value = "酒店ID")
                                             @PathVariable String hotelId) {
        Dto<Object> dto = new Dto<Object>();
        logger.debug("getVideoDescByHotelId hotelId : " + hotelId);
        if (null != hotelId && !"".equals(hotelId)) {
            HotelVideoDescVO hotelVideoDescVO = null;
            try {
                hotelVideoDescVO = itripHotelService.getVideoDescByHotelId(Long.valueOf(hotelId));
                dto = DtoUtil.returnSuccess("获取酒店视频文字描述成功", hotelVideoDescVO);
            } catch (Exception e) {
                e.printStackTrace();
                dto = DtoUtil.returnFail("获取酒店视频文字描述失败", "100214");
            }

        } else {
            dto = DtoUtil.returnFail("酒店id不能为空", "100215");
        }
        return dto;
    }

    @ApiOperation(value = "根据酒店id查询酒店特色和介绍", httpMethod = "GET",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class, notes = "根据酒店id查询酒店特色和介绍" +
            "<p>成功：success = ‘true’ | 失败：success = ‘false’ 并返回错误码，如下：</p>" +
            "<p>10210: 酒店id不能为空</p>" +
            "<p>10211: 系统异常,获取失败</p>")
    @RequestMapping(value = "/queryhoteldetails/{id}", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public Dto<ItripSearchFacilitiesHotelVO> queryHotelDetails(
            @ApiParam(required = true, name = "id", value = "酒店ID")
            @PathVariable Long id) {
        List<ItripSearchDetailsHotelVO> itripSearchDetailsHotelVOList = null;
        try {
            if (EmptyUtils.isNotEmpty(id)) {
                itripSearchDetailsHotelVOList = itripHotelService.queryHotelDetails(id);
                return DtoUtil.returnDataSuccess(itripSearchDetailsHotelVOList);
            } else {
                return DtoUtil.returnFail("酒店id不能为空", "10210");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("系统异常,获取失败", "10211");
        }
    }


    /***
     * 根据酒店id查询酒店政策 -add by donghai
     *
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "根据酒店id查询酒店政策", httpMethod = "GET",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class, notes = "根据酒店id查询酒店政策" +
            "<p>成功：success = ‘true’ | 失败：success = ‘false’ 并返回错误码，如下：</p>" +
            "<p>10208: 酒店id不能为空</p>" +
            "<p>10209: 系统异常,获取失败</p>")
    @RequestMapping(value = "/queryhotelpolicy/{id}", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public Dto<ItripSearchFacilitiesHotelVO> queryHotelPolicy(
            @ApiParam(required = true, name = "id", value = "酒店ID")
            @PathVariable Long id) {
        ItripSearchPolicyHotelVO itripSearchPolicyHotelVO = null;
        try {
            if (EmptyUtils.isNotEmpty(id)) {
                itripSearchPolicyHotelVO = itripHotelService.queryHotelPolicy(id);
                return DtoUtil.returnDataSuccess(itripSearchPolicyHotelVO.getHotelPolicy());
            } else {
                return DtoUtil.returnFail("酒店id不能为空", "10208");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("系统异常,获取失败", "10209");
        }
    }


    /***
     * 根据酒店id查询酒店设施 -add by donghai
     *
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "根据酒店id查询酒店设施", httpMethod = "GET",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class, notes = "根据酒店id查询酒店设施" +
            "<p>成功：success = ‘true’ | 失败：success = ‘false’ 并返回错误码，如下：</p>" +
            "<p>10206: 酒店id不能为空</p>" +
            "<p>10207: 系统异常,获取失败</p>")
    @RequestMapping(value = "/queryhotelfacilities/{id}", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public Dto<ItripSearchFacilitiesHotelVO> queryHotelFacilities(
            @ApiParam(required = true, name = "id", value = "酒店ID")
            @PathVariable Long id) {
        ItripSearchFacilitiesHotelVO itripSearchFacilitiesHotelVO = null;
        try {
            if (EmptyUtils.isNotEmpty(id)) {
                itripSearchFacilitiesHotelVO = itripHotelService.getItripHotelFacilitiesById(id);
                return DtoUtil.returnDataSuccess(itripSearchFacilitiesHotelVO.getFacilities());
            } else {
                return DtoUtil.returnFail("酒店id不能为空", "10206");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("系统异常,获取失败", "10207");
        }
    }





}
